package com.caojing.syby.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.syby.R;
import com.caojing.syby.bean.home.RowsBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CaoJing on 2016/10/5.
 */
/*home页下面的gridview的适配器，十元购和值得逛页面可以复用此adapter*/
public class HomeGvAdapter extends BaseAdapter {
    private List<RowsBean> mlist;
    private Context mContext;

    public HomeGvAdapter(List<RowsBean> list, Context context) {
        this.mlist = list;
        this.mContext = context;
    }
//   打算是用于数据的刷新
//    public void setData(List<RowsBean> list){
//        this.mlist=mlist;
//        notifyDataSetChanged();
//    }


    @Override
    public int getCount() {
        return mlist != null ? mlist.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mlist != null ? mlist.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_fragment_gv_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(mContext).load(mlist.get(position).getProductImg()).centerCrop().thumbnail(0.1f).placeholder(R.mipmap.jky_bg).error(R.mipmap.jky_bg).into(holder.homeFragmentGvItemIv);
        holder.homeFragmentGvItemTvTitle.setText(mlist.get(position).getName());
        holder.homeFragmentGvItemTvCurrentPrice.setText("¥"+mlist.get(position).getNewPrice());
        holder.homeFragmentGvItemTvOriginalPrice.setText(""+mlist.get(position).getOldPrice());
        holder.homeFragmentGvItemTvSell.setText("已售出"+mlist.get(position).getSaleTotal()+"件");
        holder.homeFragmentGvItemTvDiscount.setText(mlist.get(position).getDiscount()+"折");
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.home_fragment_gv_item_iv)
        ImageView homeFragmentGvItemIv;
        @Bind(R.id.home_fragment_gv_item_tvTitle)
        TextView homeFragmentGvItemTvTitle;
        @Bind(R.id.home_fragment_gv_item_tvCurrentPrice)
        TextView homeFragmentGvItemTvCurrentPrice;
        @Bind(R.id.home_fragment_gv_item_tvOriginalPrice)
        TextView homeFragmentGvItemTvOriginalPrice;
        @Bind(R.id.home_fragment_gv_item_tvDiscount)
        TextView homeFragmentGvItemTvDiscount;
        @Bind(R.id.home_fragment_gv_item_tvSell)
        TextView homeFragmentGvItemTvSell;
        @Bind(R.id.home_fragment_gv_item_tvFrom)
        TextView homeFragmentGvItemTvFrom;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
